﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="inet.db.sms2.IMUtils" %>
<%@ page import="inet.db.sms2.IMNick" %>
<%@ page import="inet.util.*" %>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<jsp:useBean id="imNickDAO" class="inet.db.sms2.IMNickDAO" />
<link href="../include/style.css" rel="stylesheet" type="text/css">
<body marginheight="0" topmargin="0" leftmargin="0" marginwidth="0"  bottommargin="0">

<%	String contextPath=request.getContextPath();
    String logout = request.getParameter("logout");
    if ("ok".equalsIgnoreCase(logout)) {
    	%>
    	<script type="text/javascript">
		
			if (window.top.ifrmSMS != undefined) {
				window.top.ifrmSMS.location.href="sendBox.jsp";
				
			}
			if (window.top.ifrmSMS_new != undefined) {
				window.top.ifrmSMS_new.location.href="khung_gui_tin.jsp";
											
			}
			if (window.top.QA != undefined) {
				window.top.QA.location.href="../qa/viewAllQuestion.jsp";
				
			}
			if (window.top.frmFORUM != undefined) {
				window.alert("reload");
				window.top.frmFORUM.location.reload(true);
				
			}
		
	</script>
    	<%
    }
    
	//System.out.println(request.getRemoteAddr() + "  requests  " + request.getRequestURI());
	String[] msgerr = {"", "Tên nick không đúng", "Nick đã bị khoá", "Mã kiểm tra không đúng", "Mật khẩu không đúng",""};
    String login = request.getParameter("login");
    int loginStatus = 0;
    IMNick imNick = null;
    if(session.getAttribute("NICK_SMS") != null) {
       imNick=(IMNick)session.getAttribute("NICK_SMS");
       session.setAttribute("NICK_SMS", imNick);  
    }else{ 
        String usrName = null;
        String usrPassword = null;
        Cookie[] cookies = request.getCookies();
        
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                String cookieName = cookie.getName();
                if (cookieName.equals("USERNAME")) {
                    usrName = cookie.getValue();
                } else if (cookieName.equals("PASSWORD")) {
                    usrPassword = cookie.getValue();
                }
            }
        }
        if ((usrName != null) && (usrPassword != null)) {
          		imNick = imNickDAO.getRow(usrName);
	    			// Neu tim thay
    			if (imNick != null) {
    				// Neu dung password
    				if (imNick.getPassword().equals(usrPassword)) {
    					session.setAttribute("NICK_SMS", imNick);
				            	session.setMaxInactiveInterval(900);
				            	System.out.println(usrName +" Login Auto OK!");
				            	%>
				            	<script type="text/javascript">
								
								if (window.top.ifrmSMS != undefined) {
									window.top.ifrmSMS.location.href="sendBox.jsp";
										
								}
								</script>
								<% 
    				} 
    			}	
        }else{
		    String nickname = request.getParameter("nick0");
	    	String password = request.getParameter("pass");
	    	String checkCode = request.getParameter("checkcode");
    		//check user name nguoi dung nhap
    		if(nickname != null && !"".equals(nickname) 
    			&& password != null && !"".equals(password)
    			&& checkCode != null && !"".equals(checkCode) ) {
    		try {
	    			// Tim nickname trong db
	    			imNick = imNickDAO.getRow(nickname);
	    			// Neu tim thay
	    			if (imNick != null) {
	    				// Neu dung password
	    				if (imNick.getPassword().equals(Md5.Hash(password))) {
	    					// Neu ma kiem tra dung
    						if (checkCode.equals((String)session.getAttribute("auth-code"))) {
		    					if (imNick.getStatus() <= 0) {
				        			loginStatus = 2;	// Nick da bi khoa
				        			
				        		} else {
				        			// Dang nhap thanh cong
					            	int autoLoginExpire = (60 * 60 );
					            	session.setAttribute("NICK_SMS", imNick);
					            	Cookie nameCookie = new Cookie("USERNAME", nickname);
					            	nameCookie.setMaxAge(autoLoginExpire);
						        	nameCookie.setPath("/");
						        	nameCookie.setDomain(".sms.vn");
						        	Cookie passwordCookie = new Cookie("PASSWORD",Md5.Hash(password));
						        	passwordCookie.setMaxAge(autoLoginExpire);
						        	passwordCookie.setPath("/");
						        	passwordCookie.setDomain(".sms.vn");
						        	response.addCookie(nameCookie);
						        	response.addCookie(passwordCookie);
					            	
					            	System.out.println(nickname +" Login OK!");
					            	%>
					            	<script type="text/javascript">
									
									if (window.top.ifrmSMS != undefined) {
										window.top.ifrmSMS.location.href="sendBox.jsp";
											
									}
									</script>
					            	<%
				        		}
			    			} else {
			    				loginStatus = 3;	// Ma kiem tra khong dung
			    			}
	    				} else {
	    					loginStatus = 4;	// Mat khau khong dung
	    				}
	    			} else {
	    				loginStatus = 1;	// Ten nick khong dung
	    			}
    		} catch (Exception e) {e.printStackTrace();}
    		}
    	}	
   } 	
		       
	String CHECK_CODE = IMUtils.generateRandomString(5);
    session.setAttribute("auth-code", CHECK_CODE);
	
	if (session.getAttribute("NICK_SMS") == null) {
	// Neu chua dang nhap
%>
 
<form name="frmLogin" method="post">
	<table width="100%"  border="0" cellspacing="0" cellpadding="0">	
	<input type="hidden" name="login" value="LOGIN">
	  <tr>
		<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td width="35%" align="left" class="white-text-login" height="40">T&ecirc;n</td>
				<td width="65%" align="left">										
				<input name="nick0" type="text" size="15" maxlength="40" onKeyPress="frmKeyPress(event)" autocomplete="off">
				</td>
			  </tr>
			  <tr>
				<td width="35%" align="left" class="white-text-login">M&#7853;t kh&#7849;u</td>
				<td width="65%" align="left">										
				<input name="pass" type="password" size="15" maxlength="40" onKeyPress="frmKeyPress(event)" autocomplete="off">
				</td>
			  </tr>
			  <tr>
				<td>&nbsp;</td>
				<td height="40" align="left"><img src="<%=request.getContextPath() %>/login/<%=Md5.Hash(CHECK_CODE)%>.gif" border="0"></td>
			  </tr>
			</table>
		</td>
	  </tr>
	  <tr>
		<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td width="35%" align="left" class="white-text-login">M&atilde;<br> ki&#7875;m tra </td>
				<td width="43%%" align="left">											
					<input name = "checkcode" type="text" size="7" maxlength="10" onKeyPress="frmKeyPress(event)">
				</td>
				<td align="left"><a href="javascript: frmSubmit();" >
				<img src="<%=request.getContextPath() %>/images/login-button.gif" border="0"></a></td>
			  </tr>
			</table>
		</td>
	</tr>
	  <tr>
		<td height="12"></td>
	  </tr>
	  <tr>
		<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td width="5">&nbsp;</td>
				<td width="20" align="left"><img src="<%=request.getContextPath() %>/images/lock.png"></td>
				<td align="left" height="22"><a href="http://sms.vn/login/" class="white-text-link">Qu&ecirc;n m&#7853;t kh&#7849;u</a></td>
			  </tr>
			</table>
		</td>
	  </tr>
	   <tr>
		<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td width="5">&nbsp;</td>
				<td width="20" align="left"><img src="<%=request.getContextPath() %>/images/user2.png" width="16" height="16"></td>
				<td align="left" height="22"><a href="http://sms.vn/register/" target="_top"  class="white-text-link">&#272;&#259;ng k&yacute; s&#7917; d&#7909;ng d&#7883;ch v&#7909;</a></td>
			  </tr>
			</table>
		 </td>
	  </tr>
	</table>	  
</form>

 

<%
if("LOGIN".equals(login) && session.getAttribute("NICK_SMS") == null&&loginStatus>0){
	//System.out.println(msgerr[loginStatus]);
    	 out.println("<script language=\"Javascript\">window.alert('"+msgerr[loginStatus]+"') </script>");
}
 %>	
	<script>
	
	// Check string is blank
	function isblank(s)
	{
		for(var i = 0; i < s.length; i++) {
			var c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
		return true;
	}
	
	function checkForm(frm) {
		var msg = "SMS.vn thông báo:";
		msg += "\n-------------------------------\n";
		if (isblank(frm.nick0.value)) {
			msg += "Bạn chưa nhập tên đăng nhập\n";
			frmLogin.nick0.focus();
			alert(msg);
			return false;
		}
		if (isblank(frm.pass.value)) {
			msg += "Bạn chưa nhập mật khẩu đăng nhập\n";
			frmLogin.pass.focus();
			alert(msg);
			return false;
		}
		if (isblank(frm.checkcode.value)) {
			msg += "Bạn chưa nhập mã kiểm tra\n";
			frmLogin.checkcode.focus();
			alert(msg);
			return false;
		}

		return true;
	}
	
	window.onload = nickFocus;
	
	function nickFocus() {
		if (frmLogin.nick0 != undefined) {
			frmLogin.nick0.focus();
		}
	}

	function frmKeyPress(p_oEvent) {
		if(p_oEvent.keyCode == 13) {
			frmSubmit();
		}
    }
    
    function frmSubmit() {
    	if (checkForm(frmLogin)) {
			frmLogin.login.value="LOGIN";
			frmLogin.submit();
		}
    }
</script>
	
<%}else{
	imNick = (IMNick) session.getAttribute("NICK_SMS");
	String sNickName = imNick.getNickname();
	String sMobileNumber = imNick.getMobileNumber();
	
	session.setAttribute("NICK",sNickName);
%>



<table width="201" border="0" cellspacing="0" cellpadding="0" height="159" valign="top" background="<%=request.getContextPath()%>/images/login-backdrop.gif" >
  <tr>
	<td><img src="<%=request.getContextPath()%>/images/top-login.jpg"></td>
  </tr>
  <tr>
	<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td width="5">&nbsp;</td>
		<td width="20" align="left"><img src="<%=request.getContextPath()%>/images/user2.png" width="16" height="16"></td>
		<td align="left" height="25" class="black-text2"><%=sNickName%> <a href="logout.jsp" target="_top" class="white-text-link">[logout]</a></td>
	  </tr>
	</table>
	</td>
  </tr>
  <tr>
	<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td width="5">&nbsp;</td>
		<td width="20" align="left"><img src="<%=request.getContextPath()%>/images/dt-icon2.png" width="19" height="19"></td>
		<td align="left" height="25" class="black-text2"><%=sMobileNumber%></td>
	  </tr>
	</table>
	</td>
  </tr>
  <tr>
	<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="5">&nbsp;</td>
			<td align="center" style="font:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#80FFFF ">-------------------------------------</td>
			<td width="5">&nbsp;</td>
		  </tr>
		</table>
	</td>
  </tr>
  <tr>
	<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="5">&nbsp;</td>
			<td width="8" align="left" height="25"><img src="<%=request.getContextPath()%>/images/icon-vuong.gif"></td>
			<td align="left" width="90"><a href="<%=request.getContextPath()%>/send/main_inbox.jsp" target="_top"  class="white-text-login">Inbox</a></td>
			<td width="8" align="left"><img src="<%=request.getContextPath()%>/images/icon-vuong.gif"></td>
			<td align="left"><a href="<%=request.getContextPath()%>/send/main_outbox.jsp" target="_top" class="white-text-login">Outbox</a></td>
		  </tr>
		</table>
		</td>
  </tr>
  <tr>
	<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="5">&nbsp;</td>
			<td width="8" align="left" height="25"><img src="<%=request.getContextPath()%>/images/icon-vuong.gif"></td>
			<td align="left" width="90"><a href="<%=request.getContextPath()%>/send/profile.jsp" target="_top" class="white-text-login">Profile</a></td>
			<td width="8" align="left"><img src="<%=request.getContextPath()%>/images/icon-vuong.gif"></td>
			<td align="left"><a href="<%=request.getContextPath()%>/send/compose.jsp" target="_top"  class="white-text-login">Send SMS </a></td>
		  </tr>
		</table>
		</td>
  </tr>
  <tr>
	<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="5">&nbsp;</td>
			<td width="8" align="left" height="25"><img src="<%=request.getContextPath()%>/images/icon-vuong.gif"></td>
			<td align="left" width="90"><a href="<%=request.getContextPath()%>/contact/" class="white-text-login" target="_top" >Contact</a></td>
			<td width="8" align="left"><img src="<%=request.getContextPath()%>/images/icon-vuong.gif"></td>
			<td align="left"><a href="<%=request.getContextPath()%>/offline/" target="_top"  class="white-text-login">Offline </a></td>
		</tr>
		</table>
		</td>
  </tr>

</table>
						
						
						
	
<%} %>	
</body>